package edu.zut.cs.software.cms.module.course.service.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.stereotype.Service;

import edu.zut.cs.software.cms.base.service.impl.GenericManagerImpl;
import edu.zut.cs.software.cms.module.course.dao.CourseDao;
import edu.zut.cs.software.cms.module.course.domain.Course;
import edu.zut.cs.software.cms.module.course.service.CourseManager;

/**
 * 
 * @author 24372
 *
 */
@Service(value = "courseManager")
@Transactional
public class CourseManagerImpl extends GenericManagerImpl<Course, Long> implements CourseManager {
	CourseDao coursedao;

	@Autowired
	public void setCourseDao(CourseDao coursedao) {
		this.coursedao = coursedao;
		this.dao = this.coursedao;
	}

	@Override
	public List<Course> FindByFullname(String fullname) {
		// 创建查询条件数据对象
		Course queryObject = new Course();
		queryObject.setEntityName(fullname);
		// 创建匹配器，即如何使用查询条件
		ExampleMatcher matcher = ExampleMatcher.matching() // 构建对象
				.withStringMatcher(StringMatcher.CONTAINING) // 改变默认字符串匹配方式：模糊查询
				.withMatcher("fullname", GenericPropertyMatchers.contains()) // 姓名采用“开始匹配”的方式查询
				.withIgnorePaths("dateCreated", "dateModified"); // 忽略属性：是否关注。因为是基本类型，需要忽略掉
		// 创建实例并查询
		Example<Course> ex = Example.of(queryObject, matcher);
		List<Course> result = this.coursedao.findAll(ex);
		return result;
	}

	@Override
	public boolean DeleteById(long id) {
		coursedao.deleteById(id);
		return true;
	}

	@Override
	public boolean modifyById(long id, String code, String name, String teaching, String credit) {
		// TODO Auto-generated method stub
		Course course=new Course();
		course=coursedao.modifyById(id);
		course.setName(name);
		course.setTeaching(teaching);
		return true;
	}
	@Override
	public boolean IfExist(String name) {
		//数据存在返回false
		//数据不存在返回true
		if((coursedao.IfOneExist(name))>0) 
			return false;
		else
			return true;
	}
}
